package org.grow.took.service;

import org.grow.took.dao.UserInfoDao;
import org.grow.took.entity.RoleInfo;
import org.grow.took.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: xwg
 * @CreateDate: 2021/9/30
 */

@Service
public class UserInfoService implements UserDetailsService {
    @Autowired
    private UserInfoDao userInfoDao;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        UserInfo userInfo = userInfoDao.findByUsername(s);
        System.out.println("从数据库查询 userInfo : "+userInfo);
        if (userInfo == null) {
            throw new UsernameNotFoundException("数据库未找到用户名");
        }
//        继续查询出对应的角色信息
        List<RoleInfo> roles = userInfoDao.queryRolesByUsername(s);
        userInfo.setAuthorities(roles);
        System.out.println("从数据库装载 userInfo : "+userInfo);
        return userInfo;
    }
}
